﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Secured_Users : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserRole"].Equals("Admin"))
            Master.LnkEnableAdmin = true;
        conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
        if (conn.State != ConnectionState.Open)
        {
            try
            {
                conn.Open();
            }
            catch (SqlException ex)
            {
                ltMsg.Text = ex.Message;
            }
        }
        if (!Page.IsPostBack)
        {
            FillDataGrid();
        }
    }
    private void FillDataGrid()
    {
        cmd = new SqlCommand("select UserID, UserName, DOB, EmailAddr, SecretQuestion, LastLogin, IsApproved from HAT_UserAccounts", conn);
        da = new SqlDataAdapter(cmd.CommandText, conn);
        ds = new DataSet();
        try
        {
            da.Fill(ds, "NewTable");
            grdUsers.DataSource = ds.Tables["NewTable"].DefaultView;
            grdUsers.DataBind();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    protected void grdUsers_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        grdUsers.EditIndex = e.NewEditIndex;
        FillDataGrid();
    }
    protected void grdUsers_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        grdUsers.EditIndex = -1;
        FillDataGrid();
    }
    protected void grdUsers_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)grdUsers.Rows[e.RowIndex];
        int UserID = Int32.Parse(grdUsers.DataKeys[e.RowIndex].Value.ToString());
        CheckBox chkisApproved = (CheckBox)row.FindControl("chkIsApproved");
        cmd = new SqlCommand("update HAT_UserAccounts set IsApproved=@isApproved where UserID=@UserID", conn);
        cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = UserID;
        cmd.Parameters.Add("@isApproved", SqlDbType.Bit).Value = chkisApproved.Checked;
        try
        {
            cmd.ExecuteNonQuery();
            grdUsers.EditIndex = -1;
            FillDataGrid();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    protected void grdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Type cstype = this.GetType();
        GridViewRow row = (GridViewRow)grdUsers.Rows[e.RowIndex];
        int UserID = Int32.Parse(grdUsers.DataKeys[e.RowIndex].Value.ToString());
        cmd = new SqlCommand("DELETE FROM HAT_UserAccounts WHERE UserID=@ID", conn);
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = UserID;
        try
        {
            Page.ClientScript.RegisterClientScriptBlock(cstype, "DeleteUser", "alert('Selected Account will be Deleted')", true);
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
        FillDataGrid();
    }
    protected void grdUsers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        Type cstype = this.GetType();
        GridViewRow row = (GridViewRow)grdUsers.Rows[e.NewSelectedIndex];
        int UserID = Int32.Parse(grdUsers.DataKeys[e.NewSelectedIndex].Value.ToString());
        ltMsg.Text = "Select UserID:" + UserID.ToString();
        Response.Redirect("~/Secured/EditUserAccount.aspx?ID=" + UserID, false);
    }
}

//using System;
//using System.Collections;
//using System.Configuration;
//using System.Data;
//using System.Linq;
//using System.Web;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.HtmlControls;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
//using System.Data.SqlClient;